package com.hsjk.szwj.module.train.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hsjk.szwj.framework.common.pojo.PageParam;
import com.hsjk.szwj.framework.common.pojo.PageResult;
import com.hsjk.szwj.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.hsjk.szwj.framework.tenant.core.aop.TenantIgnore;
import com.hsjk.szwj.module.train.controller.admin.vo.AdminCreateOrUpdateSkuBindReqVO;
import com.hsjk.szwj.module.train.controller.admin.vo.AdminCreateProductSkuReqVO;
import com.hsjk.szwj.module.train.controller.admin.vo.AdminCreateProductSpuReqVO;
import com.hsjk.szwj.module.train.controller.admin.vo.AdminProductSkuPageReqVO;
import com.hsjk.szwj.module.train.controller.admin.vo.AdminProductSkuPageRespVO;
import com.hsjk.szwj.module.train.controller.admin.vo.AdminProductSpuDetailRespVO;
import com.hsjk.szwj.module.train.controller.admin.vo.AdminProductSpuPageReqVO;
import com.hsjk.szwj.module.train.controller.admin.vo.AdminProductSpuPageRespVO;
import com.hsjk.szwj.module.train.controller.admin.vo.AdminUpdateProductSkuReqVO;
import com.hsjk.szwj.module.train.controller.admin.vo.AdminUpdateProductSpuReqVO;
import com.hsjk.szwj.module.train.controller.app.vo.product.AllNormalSpuPageReqVO;
import com.hsjk.szwj.module.train.controller.app.vo.product.SpuListRespVO;
import com.hsjk.szwj.module.train.convert.ProductConvert;
import com.hsjk.szwj.module.train.dal.dataobject.product.ProductCategoryDO;
import com.hsjk.szwj.module.train.dal.dataobject.product.ProductSkuBindDO;
import com.hsjk.szwj.module.train.dal.dataobject.product.ProductSkuDO;
import com.hsjk.szwj.module.train.dal.dataobject.product.ProductSpuDO;
import com.hsjk.szwj.module.train.dal.mysql.product.IProductCategoryMapper;
import com.hsjk.szwj.module.train.dal.mysql.product.IProductSkuBindMapper;
import com.hsjk.szwj.module.train.dal.mysql.product.IProductSkuMapper;
import com.hsjk.szwj.module.train.dal.mysql.product.IProductSpuMapper;
import com.hsjk.szwj.module.train.dal.mysql.product.IProductSpuTagMappingMapper;
import com.hsjk.szwj.module.train.service.IProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ec50n9
 **/
@Service
@Slf4j
public class ProductServiceImpl implements IProductService {

    @Resource
    private IProductCategoryMapper productCategoryMapper;

    @Resource
    private IProductSpuMapper productSpuMapper;

    @Resource
    private IProductSkuMapper productSkuMapper;

    @Resource
    private IProductSkuBindMapper productSkuBindMapper;

    @Resource
    private IProductSpuTagMappingMapper productSpuTagMappingMapper;

    @Override
    public List<ProductCategoryDO> getProductCategoryList() {
        return productCategoryMapper.selectList();
    }

    @Override
    public PageResult<ProductSpuDO> getNormalProductSpuPageByCategoryId(Long categoryId, PageParam pageParam) {
        return productSpuMapper.selectNormalSpuPageByCategoryId(categoryId, pageParam);
    }

    @Override
    public PageResult<SpuListRespVO> getNormalProductSpuPageByCarType(String carType, PageParam pageParam) {
        PageResult<ProductSpuDO> productSpuDOPageResult = productSpuMapper.selectNormalSpuPageByCarType(carType,pageParam);
        List<SpuListRespVO> listRespVOS = ProductConvert.INSTANCE.convertSpu(productSpuDOPageResult.getList());
        if (CollUtil.isNotEmpty(listRespVOS)) {
            for (SpuListRespVO respVO : listRespVOS) {
                List<String> tags = productSpuTagMappingMapper.getTagsBySpuId(respVO.getId());
                respVO.setTags(tags);
            }
        }
        return new PageResult<>(listRespVOS, productSpuDOPageResult.getTotal());
    }


    @Override
    public PageResult<SpuListRespVO> getNormalProductSpuPage(PageParam pageParam) {
        PageResult<ProductSpuDO> productSpuDOPageResult = productSpuMapper.selectNormalSpuPage(pageParam);
        List<SpuListRespVO> listRespVOS = ProductConvert.INSTANCE.convertSpu(productSpuDOPageResult.getList());
        if (CollUtil.isNotEmpty(listRespVOS)) {
            for (SpuListRespVO respVO : listRespVOS) {
                List<String> tags = productSpuTagMappingMapper.getTagsBySpuId(respVO.getId());
                respVO.setTags(tags);
            }
        }
        return new PageResult<>(listRespVOS, productSpuDOPageResult.getTotal());
    }



    @Override
    public ProductSpuDO getNormalProductSpuById(Long id) {
        return productSpuMapper.selectById(id);
    }

    @Override
    public List<ProductSkuDO> getProductSkuListBySpuId(Long spuId) {
        return productSkuMapper.selectListBySpuId(spuId);
    }

    @Override
    public ProductSkuDO getProductSkuById(Long id) {
        return productSkuMapper.selectById(id);
    }

    @Override
    @TenantIgnore
    public ProductSkuDO getCourseBindProductSkuById(Long id) {
        return productSkuMapper.selectById(id);
    }

    @Override
    public List<ProductSkuBindDO> getProductSkuBindListBySkuId(Long skuId) {
        return productSkuBindMapper.selectListBySkuId(skuId);
    }

    @Override
    @TenantIgnore
    public List<ProductSkuBindDO> getCourseBindProductSkuBindListBySkuId(Long skuId) {
        return productSkuBindMapper.selectListBySkuId(skuId);
    }

    @Override
    public PageResult<AdminProductSpuPageRespVO> getProductSpuPageList(AdminProductSpuPageReqVO reqVO) {
        PageResult<ProductSpuDO> productSpuDOPageResult = productSpuMapper.selectPage(reqVO);
        return ProductConvert.INSTANCE.convertAdminProductSpuPageRespVO(productSpuDOPageResult);
    }

    @Override
    public AdminProductSpuDetailRespVO getProductSpuDetailById(Long spuId) {
        ProductSpuDO productSpuDO = productSpuMapper.selectById(spuId);
        return ProductConvert.INSTANCE.convertAdminProductSpuDetailRespVO(productSpuDO);
    }

    @Override
    public PageResult<AdminProductSkuPageRespVO> getProductSkuPageList(AdminProductSkuPageReqVO reqVO) {
        Page<AdminProductSkuPageReqVO> page = new Page<>(reqVO.getPageNo(), reqVO.getPageSize());
        IPage<AdminProductSkuPageRespVO> productSkuPageList = productSkuMapper.getProductSkuPageList(page, reqVO);
        return new PageResult<>(productSkuPageList.getRecords(), productSkuPageList.getTotal());
    }

    @Override
    public Boolean createOrUpdateProductSkuBind(AdminCreateOrUpdateSkuBindReqVO reqVO) {
        ProductSkuBindDO productSkuBindDO = productSkuBindMapper.selectOne(new LambdaQueryWrapperX<ProductSkuBindDO>().eq(ProductSkuBindDO::getBindId, reqVO.getBindId()).eq(ProductSkuBindDO::getSkuId, reqVO.getSkuId()).eq(ProductSkuBindDO::getBindType, reqVO.getBindType()));
        if (ObjectUtil.isNotNull(productSkuBindDO)) {
            ProductSkuBindDO updateDO = new ProductSkuBindDO();
            updateDO.setId(productSkuBindDO.getId());
            updateDO.setBindTitle(reqVO.getBindTitle());
            productSkuBindMapper.updateById(updateDO);
        } else {
            productSkuBindMapper.insert(ProductConvert.INSTANCE.convertProductSkuBindDO(reqVO));
        }
        return Boolean.TRUE;
    }

    @Override
    public Boolean deleteProductSkuBind(Long id) {
        productSkuBindMapper.deleteById(id);
        return Boolean.TRUE;
    }

    @Override
    public List<String> getTagsBySpuId(Long spuId) {
        return productSpuTagMappingMapper.getTagsBySpuId(spuId);
    }

    @Override
    public Boolean createProductSku(AdminCreateProductSkuReqVO reqVO) {
        ProductSkuDO productSkuDO = ProductConvert.INSTANCE.convertProductSkuDO(reqVO);
        int insert = productSkuMapper.insert(productSkuDO);
        return insert > 0 ? Boolean.TRUE : Boolean.FALSE;
    }

    @Override
    public Boolean updateProductSku(AdminUpdateProductSkuReqVO reqVO) {
        ProductSkuDO productSkuDO = ProductConvert.INSTANCE.convertProductSkuDO(reqVO);
        int update = productSkuMapper.updateById(productSkuDO);
        return update > 0 ? Boolean.TRUE : Boolean.FALSE;
    }

    @Override
    public Boolean deleteProductSku(Long id) {
        int result = productSkuMapper.deleteById(id);
        return result > 0 ? Boolean.TRUE : Boolean.FALSE;
    }

    @Override
    public Boolean createProductSpu(AdminCreateProductSpuReqVO reqVO) {
        ProductSpuDO productSpuDO = ProductConvert.INSTANCE.convertProductSpuDO(reqVO);
        productSpuDO.setCarType(reqVO.getCarType());
        int insert = productSpuMapper.insert(productSpuDO);
        if (insert > 0) {
            return Boolean.TRUE;
        } else {
            return Boolean.FALSE;
        }
    }

    @Override
    public Boolean updateProductSpu(AdminUpdateProductSpuReqVO reqVO) {
        ProductSpuDO productSpuDO = ProductConvert.INSTANCE.convertProductSpuDO(reqVO);
        productSpuDO.setCarType(reqVO.getCarType());
        int updateResult = productSpuMapper.updateById(productSpuDO);
        if (updateResult > 0) {
            return Boolean.TRUE;
        } else {
            return Boolean.FALSE;
        }
    }
}
